package com.wt.servlet;

import com.alibaba.fastjson.JSONObject;
import com.wt.controller.WqunAccountController;
import com.wt.entity.WqunAccount;
import com.wt.util.R.R;
import com.wt.util.ResultMess.ResultMess;
import com.wt.util.axios.Axios;
import com.wt.util.axios.JsonToEntity;
import com.wt.util.axios.Return;
import com.wt.util.exceptionhandler.WQUNException;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

@WebServlet({"/wqunAccountController/changeAccountPhone","/wqunAccountController/changeAccount",
        "/wqunAccountController/findAccountPage","/wqunAccountController/deleteById",
        "/wqunAccountController/findAccountPublicById"})
public class AccountServlet extends HttpServlet {
    WqunAccountController wqunAccountController = new WqunAccountController();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject jsonObject = null;
        HashMap<String, String> map = null;
        WqunAccount wqunAccount = new WqunAccount();

       // System.out.println(req.getRequestURL().substring(req.getRequestURL().lastIndexOf("/")+1));
        switch (req.getRequestURL().substring(req.getRequestURL().lastIndexOf("/")+1)){
            case "changeAccountPhone":
                try {
                    jsonObject = Axios.post(req);//Json接受
                } catch (IOException e) {
                    Return.R(resp,R.error().message(ResultMess.DARA_FORMAT_ERROR.getDesc()));
                }
                try {
                    map = JsonToEntity.wqunAccount(jsonObject);//将json转换成Object
                } catch (WQUNException e) {
                    Return.R(resp,R.error().message(e.getMsg()));//异常返回
                }

                try {
                    BeanUtils.populate(wqunAccount, map);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
                Return.R(resp,wqunAccountController.updatePass(wqunAccount));
                break;
            case "changeAccount":
                //System.out.println("changeAccount");
                try {
                    jsonObject = Axios.post(req);//Json接受
                } catch (IOException e) {
                    Return.R(resp,R.error().message(ResultMess.DARA_FORMAT_ERROR.getDesc()));
                }
                try {
                    map = JsonToEntity.wqunAccount(jsonObject);//将json转换成Object
                } catch (WQUNException e) {
                    Return.R(resp,R.error().message(e.getMsg()));//异常返回
                }
                try {
                    BeanUtils.populate(wqunAccount, map);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
                Return.R(resp,wqunAccountController.update(wqunAccount));
                break;
            case "findAccountPage":
                int pageSize = Integer.parseInt(req.getParameter( "pageSize"));
                int currentPage = Integer.parseInt(req.getParameter( "currentPage"));
                String selectValue = null;
                String inputValue = null;
                if(req.getParameter( "selectValue") != null && req.getParameter( "selectValue").length() != 0){
                    selectValue = req.getParameter( "selectValue");
                }
                if(req.getParameter( "inputValue") != null && req.getParameter( "inputValue").length() != 0){
                    inputValue = req.getParameter( "inputValue");
                }
                Return.R(resp,wqunAccountController.findAccountPage(pageSize,currentPage,selectValue,inputValue));
                break;
            case "deleteById":
                int id = Integer.parseInt(req.getParameter( "id"));
                Return.R(resp,wqunAccountController.deleteById(id));
                break;
            case "findAccountPublicById":
                int uid = Integer.parseInt(req.getParameter( "uid"));
                Return.R(resp,wqunAccountController.findAccountPublicById(uid));
                break;
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
